unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DBAccess, UniDacVcl, DB, Uni,UniConnct,
  OracleUniProvider, UniProvider, SQLServerUniProvider, SQLiteUniProvider,
  DAScript, UniScript,UniConfig, MemDS, ODBCUniProvider, AccessUniProvider,
  AdvGrid;

type
  TForm_Main = class(TForm)
    Btnx_1: TButton;
    Btnx_2: TButton;
    UniConnection1: TUniConnection;
    SQLServerUniProvider1: TSQLServerUniProvider;
    OracleUniProvider1: TOracleUniProvider;
    SQLiteUniProvider1: TSQLiteUniProvider;
    UniScript1: TUniScript;
    Btnx_4: TButton;
    Btnx_3: TButton;
    UniQuery1: TUniQuery;
    Btnx_5: TButton;
    AccessUniProvider1: TAccessUniProvider;
    UniTransaction1: TUniTransaction;
    Btnx_6: TButton;
    procedure Btnx_1Click(Sender: TObject);
    procedure Btnx_2Click(Sender: TObject);
    procedure Btnx_4Click(Sender: TObject);
    procedure Btnx_3Click(Sender: TObject);
    procedure Btnx_5Click(Sender: TObject);
    procedure Btnx_6Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
  public
    procedure UniConfigCustomConnectionEventBySQL(AConnectionMark:string;var SQLA:string);
    procedure UniConfigCustomConnectionEventByOBJ(AConnectionMark:string;var OBJA:TUniConfig);
    procedure ListUniConfigCustomGridStylEvent(AGrid:TAdvStringGrid);
  end;

var
  Form_Main: TForm_Main;

implementation

uses
  Dialog_ListUniConfig;




{$R *.dfm}

procedure TForm_Main.Btnx_1Click(Sender: TObject);
begin
  ViewListUniConfig(dlucemEdit);
  //ViewListUniConfig(dlucemEdit,ListUniConfigCustomGridStylEvent);
end;

procedure TForm_Main.Btnx_2Click(Sender: TObject);
begin
  {UniConnection1.Connected:=False;
  begin
    if UniConnectDialog1.Execute then
    begin

    end;
  end;}
  TUniConfig.Initialize;
end;

procedure TForm_Main.Btnx_4Click(Sender: TObject);
var
  I:Integer;
  UniConfig:TUniConfig;
begin
  //UniConnection1.Connected:=True;
  UniConnection1.Connect;
  UniConfig:=TUniConfig.Create;

  try
    UniConnection1.StartTransaction;

    for I:=0 to 99  do
    begin
      UniConfig.UnixIdex:=I+2;
      UniConfig.UnixStat:=0;
      UniConfig.UnixYear:=2012;
      UniConfig.UnixMark:='ZCGL';
      UniConfig.UnixType:='SQL Server';
      UniConfig.UnixPswd:='sa';
      UniConfig.UnixUser:='sa';
      UniConfig.DataBase:='DB_YSGL';
      UniConfig.UnixServ:='.';
      UniConfig.UnixPort:='8090';
      UniConfig.IsDirect:=1;
      UniConfig.InsertDB(UniConnection1);
    end;
    UniConnection1.Commit;
  except
    UniConnection1.Rollback;
  end;


  if UniConfig<>nil then
  begin
    FreeAndNil(UniConfig);
  end;  
end;

procedure TForm_Main.Btnx_3Click(Sender: TObject);
var
  ASign:string;
  StrsA:TStringList;
  UniConnection:TUniConnection;
begin
  UniConnctEx.OnUniConnctCustomConnectionEventBySQL:=UniConfigCustomConnectionEventBySQL;
  
  ASign:='YSGL';

  UniConnection:=nil;
  UniConnection:=UniConnctEx.GetConnection(ASign);

  if UniConnection<>nil then
  begin
    StrsA:=TStringList.Create;
    UniConnection.GetTableNames(StrsA,False);
    ShowMessage(StrsA.Text);
  end;

  FreeAndNil(UniConnection);
  FreeAndNil(StrsA);
end;

procedure TForm_Main.UniConfigCustomConnectionEventBySQL(AConnectionMark: string;
  var SQLA: string);
begin
  {if AConnectionMark='YSGL' then
  begin
    SQLA:=Format('SELECT * FROM TBL_UNICONFIG WHERE UNIX_MARK=%S',[QuotedStr('YSGL')]);
  end;}
  
  SQLA:=Format('SELECT * FROM TBL_UNICONFIG WHERE UNIX_MARK=%S',[QuotedStr(AConnectionMark)]);
end;

procedure TForm_Main.Btnx_5Click(Sender: TObject);
var
  StrsA:TStringList;
  CnfgA:TUniConfig;
  UniConnectionA:TUniConnection;
begin
  CnfgA:=nil;

  if ViewListUniConfig(dlucemView,CnfgA)<>Mrok then Exit;
  if CnfgA=nil then Exit;
    
  UniConnectionA:=nil;
  UniConnectionA:=UniConnctEx.GetConnection(CnfgA);
  if UniConnectionA<>nil then
  begin
    ShowMessage('true');
    StrsA:=TStringList.Create;
    UniConnectionA.GetTableNames(StrsA,False);
    ShowMessage(StrsA.Text);
  end;

  FreeAndNil(UniConnectionA);
  FreeAndNil(CnfgA);
  FreeAndNil(StrsA);
end;

procedure TForm_Main.ListUniConfigCustomGridStylEvent(AGrid: TAdvStringGrid);
begin
  with AGrid do
  begin
    HideColumn(2);
  end;  
end;

procedure TForm_Main.Btnx_6Click(Sender: TObject);
begin
  UniConnctEx.OnUniConnctCustomConnectionEventBySQL:=UniConfigCustomConnectionEventBySQL;
    
  if not UniConnctEx.ChkConnection('XZGL') then
  begin
    ShowMessage('XZGL config is not exist');
  end;  
end;

procedure TForm_Main.FormShow(Sender: TObject);
{var
  SQLA:string;
  UniConnct:TUniConnection;
  UniQuery :TUniQuery;
  BoolA:Boolean;}
begin
  {BoolA:=False;
  BoolA:=UniConnctEx.ChkConnection('GWKX');
  if not BoolA then Exit;

  try
    UniConnct:=nil;
    UniConnct:=UniConnctEx.GetConnection('GWKX');
    //->
    SQLA:='select * from tbl_xtyh';
    UniQuery:=TUniConfig.GetUniQuery(SQLA,UniConnct);
    ShowMessageFmt('%D',[UniQuery.RecordCount]);
    //-<
  finally
    if UniConnct<>nil then
    begin
      FreeAndNil(UniConnct);
    end;
    if UniQuery1<>nil then
    begin
      FreeAndNil(UniQuery);
    end;
  end;}
end;

procedure TForm_Main.FormCreate(Sender: TObject);
begin
  UniConnctEx.ConnctMark:='GWKX,XTGL,ZBGL,XZGL-OLDX';
  UniConnctEx.ConnctMode:=cmByOBJ;
  UniConnctEx.OnUniConnctCustomConnectionEventBySQL:=UniConfigCustomConnectionEventBySQL;
  UniConnctEx.OnUniConnctCustomConnectionEventByOBJ:=UniConfigCustomConnectionEventByOBJ;
end;

procedure TForm_Main.UniConfigCustomConnectionEventByOBJ(
  AConnectionMark: string; var OBJA: TUniConfig);
begin
  if AConnectionMark='GWKX' then
  begin
    OBJA.UnixType:=CONST_PROVIDER_SQLSRV;
    OBJA.UnixPswd:='sa';
    OBJA.UnixUser:='sa';
    OBJA.UnixServ:='.';
    OBJA.DataBase:='zh2012_ysgl_xtgl';
  end;  
end;

end.
